//
//  DDCollectViewController.m
//  ialone
//
//  Created by 陆翀 on 16/1/19.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "DDCollectViewController.h"
#import "DDSoundModel.h"
#import "DDSoundTableViewCell.h"
#import "FMDB.h"
#import "DDManager.h"
#import "DDSoundDetailsViewController.h"
@interface DDCollectViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)FMDatabase *db;
@property (nonatomic ,strong)NSMutableArray *dataArray;
@end

@implementation DDCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     _dataArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    //创建我们的数据库的路径,同时拼接数据库名字
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"DDDataBase.db"];
    NSLog(@"%@",path);
    //根据路径去创建一个数据库
    _db = [FMDatabase databaseWithPath:path];
    //调用open方法打开数据库,成功返回YES,失败返回NO
    if ([_db open]) {
        NSLog(@"打开成功");
    }else
    {
        NSLog(@"打开失败");
    }
    [self setUpdata];
    self.titleLabel.text = @"收藏";
    self.titleLabel.font = [UIFont systemFontOfSize:28];
    
    [self.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.leftButton setImage:[UIImage imageNamed:@"item_cancel@2x"] forState:(UIControlStateNormal)];
    [self addTableView];
}
-(void) setUpdata
{
    FMResultSet *result = [_db executeQuery:@"SELECT * FROM CollectList"];
    while ([result next]) {
        NSString *username = [result stringForColumn:@"username"];
        NSString *thumbnail = [result stringForColumn:@"thumbnail"];
        NSString *title = [result stringForColumn:@"title"];
        NSString *author = [result stringForColumn:@"author"];
        NSString *pageid = [result stringForColumn:@"pageid"];
        NSString *category = [result stringForColumn:@"category"];
        NSString *html5 = [result stringForColumn:@"html5"];
        if ([[[DDManager sharedHandle] username] isEqualToString:username]) {
            DDSoundModel *model = [[DDSoundModel alloc]init];
            model.thumbnail = thumbnail;
            model.title = title;
            model.author = author;
            model.pageid = pageid;
            model.category = category;
            model.html5 = html5;
            [_dataArray addObject:model];
        }
    }
}
- (void)addTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[DDSoundTableViewCell class] forCellReuseIdentifier:@"DDSoundTableViewCell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDSoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DDSoundTableViewCell" forIndexPath:indexPath];
    
    DDSoundModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDSoundModel *model = self.dataArray[indexPath.row];
    DDSoundDetailsViewController *sdVC = [[DDSoundDetailsViewController alloc]init];
    sdVC.html5 = model.html5;
    sdVC.pageid = model.pageid;
    sdVC.category = model.category;
    sdVC.titlename = model.title;
    sdVC.thumbnail = model.thumbnail;
    sdVC.author = model.author;
    [self.navigationController pushViewController:sdVC animated:YES];
}
- (void)leftButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
